﻿using HotelManager.Model;
using Microsoft.EntityFrameworkCore;

namespace HotelManager.DAL
{
    public class HotelDbContext : DbContext
    {
        public HotelDbContext()
        {
        }

        public HotelDbContext(DbContextOptions options) : base(options)
        {
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var folder = Environment.SpecialFolder.LocalApplicationData;
            var path = Environment.GetFolderPath(folder);
            var DbPath = Path.Join(path, "hotel.db3");
            // 如果没有配置数据库连接字符串，这里就需要配置数据库连接串
            // 否则，就没有必要配置SQLite数据库了
            optionsBuilder.UseSqlite($"Data Source={DbPath}");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            CreateSeed(modelBuilder);
        }

        private static void CreateSeed(ModelBuilder modelBuilder)
        {
            // 默认管理员账号：admin 密码：admin
            modelBuilder.Entity<Admin>().HasData(
                new Admin {LoginId = "admin", PassWord = "123456" } // "admin" 的MD5
            );
        }

        public DbSet<Admin> Admins { get; set; }
        public DbSet<DishList> DishLists { get; set; }
        public DbSet<GuestRecord> GuestRecords { get; set; }
        public DbSet<Room> Rooms { get; set; }
        public DbSet<RoomState> RoomStates { get; set; }
        public DbSet<RoomBusiness> RoomBusinesses { get; set; }
        public DbSet<RoomType> RoomTypes { get; set; }
        public DbSet<ResideState> ResideStates { get; set; }
    }
}
